package com.sea.data.service.impl;

import com.sea.data.entity.OrderDetail;
import com.sea.data.mapper.OrderDetailMapper;
import com.sea.data.service.OrderDetailService;
import com.sea.supply.service.BuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private BuyService buyService;

    @Override
    public List<OrderDetail> findSameData(Integer[] idList) {
        return orderDetailMapper.findSameData(idList);
    }

    @Override
    public int save(OrderDetail orderDetail) {
        return orderDetailMapper.insert(orderDetail);
    }

    @Override
    public List<OrderDetail> findByOrderId(Integer orderId) {
        return orderDetailMapper.findByOrderId(orderId);
    }

    @Override
    public int usave(OrderDetail orderDetail) {
        return orderDetailMapper.update(orderDetail);
    }

    @Override
    public int delete(Integer id) {
        return orderDetailMapper.delete(id);
    }

    @Override
    public OrderDetail findById(Integer id) {
        return orderDetailMapper.findById(id);
    }

    @Override
    public List<OrderDetail> export(OrderDetail orderDetail) {
        return orderDetailMapper.export(orderDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void produce(Integer orderId) {
        updateSupplierName();
        List<OrderDetail> list = findSupplierNameByOrderId(orderId);
        if(list.size() !=0){

            String errMsg="暂未查询到信息：<br />";
            for(OrderDetail detail:list){
                errMsg+="渠道【"+detail.getOrderChannelName()+"】SKU【"+detail.getOrderSku()+"】";
            }
            throw new RuntimeException(errMsg);
        }

        List<OrderDetail> supplierList = groupByOrderDetailSupplierName(orderId);
        if(supplierList.size()==0){
            throw new RuntimeException("暂未获取到供应商信息,请联系管理员！");
        }


        for(OrderDetail detail:supplierList){

            Integer[] ids = findIdsBySupplierNameOrderId(orderId, detail.getSupplierName());
            System.out.println(detail.getSupplierName()+"-"+ids.length);
            buyService.produce(ids, detail.getSupplierName(), "人民币", detail.getPaymentType());

        }



    }

    @Override
    public Integer updateSupplierName() {
        return orderDetailMapper.updateSupplierName();
    }

    @Override
    public List<OrderDetail> findSupplierNameByOrderId(Integer orderId) {
        return orderDetailMapper.findSupplierNameByOrderId(orderId);
    }

    @Override
    public List<OrderDetail> groupByOrderDetailSupplierName(Integer orderId) {
        return orderDetailMapper.groupByOrderDetailSupplierName(orderId);
    }

    @Override
    public Integer[] findIdsBySupplierNameOrderId(Integer orderId, String supplierName) {
        return orderDetailMapper.findIdsBySupplierNameOrderId(orderId,supplierName);
    }
}
